import { getAccountByCode } from "@/miniprogram/utils/apis";
import { WeilaCore, WL_ClientType } from "weilasdk_lite";

  
Page({
    data: {
        isAgreed: false,
        avatarUrl: 'https://ai-public.mastergo.com/ai/img_res/4ce0972e4be0a3efc31581379beae6a3.jpg',
        code: '',
        isPhoneModalVisible: false,
        phoneNumber: ''
    },

    onLoad() {
        // get globalData from app instance
    },

    openServiceAgreement() {
        // 打开服务协议页面
        console.log('打开服务协议');
    },
    openPrivacyPolicy() {
        // 打开隐私政策页面
        console.log('打开隐私政策');
    },
    onAgreementChange(e: any) {
        console.log('协议同意状态:', e.detail.value.length > 0);
        
        this.setData({
            isAgreed: e.detail.value.length > 0
        });
    },

    async handleLogin(res: any) {
        // 检查是否同意协议
        if (!this.data.isAgreed) {
            wx.showToast({
                title: '请先同意服务协议和隐私政策',
                icon: 'none'
            });
            return;
        }
        
        // 获取微信登录code并进行手机号一键登录
        console.log('手机号一键登录', res.detail.code);

        try {
            let rsp = await getAccountByCode({
                wx: {
                    appid: getApp<IAppOption>().globalData.accountInfo!.miniProgram.appId,
                    openid: getApp<IAppOption>().globalData.openId!,
                    unionid: getApp<IAppOption>().globalData.unionid!,
                    sessionkey: getApp<IAppOption>().globalData.sessionKey!
                },
                code: res.detail.code
            });

            console.log('获取到的账号信息', rsp);
            if (rsp.errcode == 0) {
                let weilaCore: WeilaCore = getApp<IAppOption>().globalData.weilaCore;
                try {
                    const ret = await weilaCore.weila_login(rsp.data.account.username, rsp.data.account.passwd, rsp.data.account.countrycode, WL_ClientType.CLIENT_MINIPROGRAM, true);
                    console.log("微喇登录结果", ret);
                    wx.switchTab({
                        url: '/pages/life/life-main/life-main'
                    }).then(() => {
                        console.log("跳转成功");
                    }).catch((err) => {
                        console.error("跳转失败", err);
                    });
                } catch (err) {
                    console.error('微喇登录失败', err);
                    wx.showToast({
                        title: '微喇登录失败，请重试:' + err,
                        icon: 'error'
                    });
                }
            }else {
                wx.showToast({
                    title: "登陆失败:" + rsp.errmsg,
                    icon: 'error'
                });
            }
        }catch (err) {
            console.error('登录失败', err);
            wx.showToast({
                title: '登录失败，请重试:' + err,
                icon: 'error'
            });
        }
        
    },
    handleOtherLogin() {
        console.log('其他手机登录');
     
        // 检查是否同意协议
        if (!this.data.isAgreed) {
            wx.showToast({
                title: '请先同意服务协议和隐私政策',
                icon: 'none'
            });
            return;
        }
        
        // 获取微信登录code并进行其他手机登录
        wx.navigateTo({
            url: '/pages/login_phone/login_phone'
        });
    },

    preventTouchMove(): boolean {
        return false; // Prevents page scrolling when modal is open
    }
});